<?php

namespace App\Admin\Controllers\Sys;

use App\Models\Subscribe\Price;
use App\Models\Sys\Config;
use App\Models\Sys\Task;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class RuleController extends Controller
{
    use ModelForm;

    public $header = '更多规则设置';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $box = new Widgets\Box('签到规则设置',$this->grid()->render());
            $content->body($box->solid()->style('primary'));

            $box1 = new Widgets\Box('家长订阅币规则设置',$this->grid1()->render());
            $content->row($box1->solid()->style('primary'));

            $box1 = new Widgets\Box('礼物转校币设置',$this->form1());
            $content->body($box1->solid()->style('primary'));

        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));

        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Task::class, function (Grid $grid) {
            $grid->model()->where('type',3);
            $grid->task_id('ID')->sortable();
            $grid->name('任务名称');
            $grid->expe('经验')->editable();
            $grid->money('校币')->editable();
            $grid->gold('积分')->editable();

            $grid->actions(function(Grid\Displayers\Actions $actions){
                $actions->disableDelete();
                $actions->disableView();
            });

            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->disableExport();

        });
    }

    protected function grid1()
    {
        return Admin::grid(Price::class, function (Grid $grid) {

            $grid->setResource('/sys/price');

            $grid->id('ID')->sortable();
            $grid->monthNum('家长订阅')->display(function ($v){
                return $v . '月';
            });
            $grid->gold('积分')->editable();
            $grid->expe('经验')->editable();

            $grid->actions(function(Grid\Displayers\Actions $actions){
                $actions->disableDelete();
                $actions->disableView();
            });

            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->disableExport();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Task::class, function (Form $form) {

            $form->display('task_id', 'ID');
            $form->number('expe','经验值')->rules('required|integer');
            //$form->number('money','校币');
            $form->number('money', '校币')->rules('required|regex:/^[0-9]+([.]{1}[0-9]{1})?$/',[
                'regex' => '校币 必须是个整数或者1位小数的数字'
            ]);
            $form->number('gold','积分')->rules('required|integer');

            $form->tools(function(Form\Tools $tools){
                $tools->disableView();
                $tools->disableDelete();
            });

            $form->footer(function ($footer){
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
            });

        });
    }

    protected function form1()
    {
        $form = new Widgets\Form();

        $form->method('post');

        $proportion = Config::where('key','PROPORTION')->select(['value'])->first()->toArray();

        $form->rate('proportion', '送礼物返回校币比例')
            ->attribute('value',$proportion['value'])
            ->setWidth(2)
            ->rules('required|min:0|max:100');

        $form->action('/sys/rules/updatePro');

        return $form->render();
    }

    public function updatePro(Request $request)
    {
        $proportion = $request->proportion;
        if ($proportion){
            if ($proportion > 0 && $proportion< 100){
                $flag = Config::where('key','PROPORTION')->update(['value'=>$proportion]);
                if ($flag){

                    admin_toastr(trans('admin.update_succeeded'), 'success');

                    return back();

                }else{
                    $error = new MessageBag([
                        'title'   => trans('admin.update_failed'),
                        'message' => $msg ?? '未知错误',
                    ]);

                    return back()->with(compact('error'));
                }
            } else {
                $error = new MessageBag([
                    'title'   => trans('admin.update_failed'),
                    'message' => $msg ?? '请输入0-100的正整数',
                ]);

                return back()->with(compact('error'));
            }
        }else{
            $error = new MessageBag([
                'title'   => trans('admin.update_failed'),
                'message' => $msg ?? '数据不存在',
            ]);

            return back()->with(compact('error'));
        }
    }

}
